package com.bawei.week.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.mvvm.BaseActivity
import com.bawei.lib_base.utils.Cons
import com.bawei.week.R
import com.bawei.week.databinding.ActivityLoginBinding
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.TouchUtils
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/LoginActivity")
@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding,UserViewModel>() {
    @Autowired
    @JvmField
    var username = ""
    @Autowired
    @JvmField
    var password = ""
    override fun initView() {
        ARouter.getInstance().inject(this)

        binding.loginUsername.setText(username)

        binding.loginPassword.setText(password)

        if (!SPUtils.getInstance().getString(Cons.TOKEN).isNullOrEmpty()){
            ARouter.getInstance().build("/app/HomeActivity").navigation()
            finish()
            return
        }

        //点击登录
        binding.btnLogin.setOnClickListener {
            val username = binding.loginUsername.text.toString()
            val password = binding.loginPassword.text.toString()

            if (username.isEmpty() || password.isEmpty()){
                ToastUtils.showShort("账号或密码未输入")
                return@setOnClickListener
            }

            vm.getUserLogin(password,username)
        }

        //点击跳转到注册
        binding.tvSignAccount.setOnClickListener {
            ARouter.getInstance().build("/app/RegisterActivity").navigation()
        }
    }

    override fun initData() {
        vm.userLoginSuc.observe(this){
            ARouter.getInstance().build("/app/HomeActivity").navigation()
            SPUtils.getInstance().apply {
                put(Cons.TOKEN,it.token)
                put(Cons.UNAME,it.username)
                put(Cons.UID,it.id)
            }
        }
    }
}